import groupBaseInfoDao from "../dao/groupbaseinfoDB.js";
import {getNow, getUniAccount, getUniSessionID, httpResult} from "../utils/utils.js";
import sequelize from "../models/initSequelize.js";
import userFriendGroupDao from "../dao/userfriendgroupDB.js";
import userDao from "../dao/userDB.js";
import groupAnnounceDao from "../dao/groupannounceDB.js";
import userBaseDao from "../dao/userbaseDB.js";
//创建群
export const createGroup = async (req, res, next) => {
  let {leader, avatar, name} = req.query;
  if (!leader || !name) {
    throw new Error("请正确输入账号和名称!");
    return;
  }
  let leaderIsExisted = await userDao.isExisted(leader);
  if (!leaderIsExisted) {
    throw new Error("请正确输入账号和名称!");
    return;
  }
  let newGroupInfo;
  const t = await sequelize.transaction();
  try {
    let groupAccount = await getUniAccount();
    let sessionID = await getUniSessionID();
    newGroupInfo = await groupBaseInfoDao.addGroupBaseInfo(groupAccount, sessionID, name, avatar, getNow(), leader, t);
    await userFriendGroupDao.addUserFriendGroup(leader, groupAccount, sessionID, 1, "", t);
    await t.commit();
  } catch (e) {
    console.log(e);
    await t.rollback();
    throw new Error("创建失败请联系管理员！");
    return;
  }
  res.json(httpResult.success({
    msg: "创建成功！",
    groupData: newGroupInfo.dataValues,
  }));
  return;
};
//解散群
export const disbandGroup = async (req, res, next) => {
  let {sender, groupAccount, sessionID} = req.query;
  if (!(sender && groupAccount && sessionID)) {
    throw new Error("请正确输入群组信息!");
    return;
  }
  let groupInfo = await groupBaseInfoDao.isExistedByAccount(groupAccount);
  if (sender !== groupInfo._leader) {
    throw new Error("你没有解散群组的权限!");
    return;
  }
  if (sessionID !== groupInfo._session_id) {
    throw new Error("请输入正确的群组信息!");
    return;
  }
  const t = await sequelize.transaction();
  try {
    await groupBaseInfoDao.deleteGroupBaseInfo(groupAccount, t);
    await userFriendGroupDao.deleteUserGroup(sessionID, t);
    await groupAnnounceDao.deleteGroupAnnounceList(sessionID, t);
    await t.commit();
  } catch (e) {
    console.log(e);
    await t.rollback();
    throw new Error("创建失败请联系管理员！");
    return;
  }
  res.json(httpResult.success({
    msg: "解散成功！",
    groupAccount
  }));
  return;
};
//修改群信息
export const updateGroupBase = async (req, res, next) => {
  let {sender, groupAccount, sessionID, name, avatar} = req.query;
  if (!(sender && groupAccount && sessionID && name)) {
    throw new Error("请正确输入群组信息!");
    return;
  }
  let groupInfo = await groupBaseInfoDao.isExistedByAccount(groupAccount);
  if (sender !== groupInfo._leader) {
    throw new Error("你没有修改群组信息的权限!");
    return;
  }
  if (sessionID !== groupInfo._session_id) {
    throw new Error("请输入正确的群组信息!");
    return;
  }
  let updateRes = await groupBaseInfoDao.updateGroupBaseInfo(groupAccount, sessionID, name, avatar, groupInfo._birth, sender);
  if (updateRes[0] === 0) {
    throw new Error("请输入修改信息!");
    return;
  }
  groupInfo = await groupBaseInfoDao.isExistedByAccount(groupAccount);
  res.json(httpResult.success({
    msg: "修改成功！",
    groupInfo
  }));
  return;
};
//获取群信息
export const getGroupBase = async (req, res, next) => {
  let {groupAccount} = req.query;
  if (!groupAccount) {
    throw new Error("请输入群组账号!");
    return;
  }
  let groupInfo = await groupBaseInfoDao.isExistedByAccount(groupAccount);
  if (!groupInfo) {
    throw new Error("群组不存在!");
    return;
  }
  res.json(httpResult.success({
    msg: "查询成功！",
    groupInfo
  }));
  return;
};
// 获取群信息--传入群号数组
export const getGroupBaseList = async (req, res, next) => {

  let {groupAccountList, startTime} = req.body;
  if (!groupAccountList || groupAccountList.length === 0) {
    throw new Error("请输入群组账号!");
    return;
  }
  startTime = startTime ?? 0;
  let groupInfo = await groupBaseInfoDao.getGroupBaseInfoList(groupAccountList, startTime);
  res.json(httpResult.success({
    msg: "查询成功！",
    groupInfo
  }));
  return;
};
//修改群管理员---后续添加数据库增加
//获取群公告
export const getGroupAnnounce = async (req, res, next) => {
  let {groupAccount, sender, sessionID} = req.query;
  let isExisted = await userFriendGroupDao.isExisted(sender, "", sessionID, 1);
  if (!isExisted) {
    throw new Error("您还不是群成员");
    return;
  }
  let groupAnnounce = await groupAnnounceDao.getGroupAnnounceList(groupAccount);
  res.json(httpResult.success(groupAnnounce));
  return;
};
//修改群公告
export const updateGroupAnnounce = async (req, res, next) => {
  let {sender, groupAccount, sessionID, newAnnounce} = req.query;
  if (!(sender && groupAccount && sessionID && newAnnounce)) {
    throw new Error("请正确输入公告信息!");
    return;
  }
  let isLeader = await groupBaseInfoDao.isLeaderBySessionID(sender, sessionID);
  if (!isLeader) {
    throw new Error('您没有权限修改公告！');
    return;
  }
  let updateRes = await groupAnnounceDao.updateGroupAnnounce(groupAccount, getNow(), newAnnounce, sessionID);
  if (!updateRes[0]) {
    throw new Error('修改公告失败请联系管理员！');
    return;
  }
  res.json(httpResult.success("修改成功"));
  return;
};
//获取群成员
export const getGroupMember = async (req, res, next) => {
  let {sessionID, sender} = req.query;
  let isExisted = await userFriendGroupDao.isExisted(sender, "", sessionID, 1);
  if (!isExisted) {
    throw new Error("您还不是群成员");
    return;
  }
  let groupMember = await userFriendGroupDao.getUserFriendGroupMember(false, sessionID);
  res.json(httpResult.success(groupMember));
  return;
};
//踢出群成员
export const removeGroupMember = async (req, res, next) => {
  let {sessionID, sender, account} = req.query;
  if (sender === account) {
    throw new Error("无权踢出群主！");
    return;
  }
  let isExisted = await userFriendGroupDao.isExisted(sender, "", sessionID, 1);
  if (!isExisted) {
    throw new Error("请输入正确的账号");
    return;
  }
  isExisted = await userFriendGroupDao.isExisted(account, "", sessionID, 1);
  if (!isExisted) {
    throw new Error("请输入正确的账号");
    return;
  }
  let isLeader = await groupBaseInfoDao.isLeaderBySessionID(sender, sessionID);
  if (!isLeader) {
    throw new Error('您没有权限剔除成员！');
    return;
  }
  let deleteRes = await userFriendGroupDao.deleteUserFriend(account, "", sessionID, 1);
  if (!deleteRes) {
    throw new Error("请联系管理员删除失败！");
    return;
  }
  res.json(httpResult.success("删除成功"));
  return;
};

